package com.example.yiqinmanagersystem.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yiqinmanagersystem.entity.BanJi;
import com.example.yiqinmanagersystem.entity.Role;
import com.example.yiqinmanagersystem.entity.User;
import com.example.yiqinmanagersystem.entity.XueYuan;
import com.example.yiqinmanagersystem.service.BanjiService;
import com.example.yiqinmanagersystem.service.RoleService;
import com.example.yiqinmanagersystem.service.UserService;
import com.example.yiqinmanagersystem.service.XueYuanService;
import com.example.yiqinmanagersystem.vo.ResponseCode;
import com.example.yiqinmanagersystem.vo.ResponseResult;
import com.example.yiqinmanagersystem.vo.RoleVo;
import com.example.yiqinmanagersystem.vo.UserVo;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 **@Author:youpeng
 **Created on 2022/9/17
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private BanjiService banjiService;

    @Resource
    private XueYuanService xueYuanService;

    @Resource
    private RoleService roleService;

    /*
     * 获取用户列表, 带分页以及查询条件
     * */
    @GetMapping("/user/loadAllUser")
    public ResponseResult loadAllUser(UserVo userVo){
        Page<User> page = new Page<>(userVo.getPage(),userVo.getLimit());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(userVo.getUsername()),"username",userVo.getUsername());
        queryWrapper.like(StringUtils.isNoneBlank(userVo.getPhone()),"phone",userVo.getPhone());
        Page<User> userPage = userService.page(page, queryWrapper);
        //为id 赋值 为 name
        for (User user : page.getRecords()) {
            if(user.getBanJiId()!=null){
                //班级查库
                BanJi banJi = banjiService.getById(user.getBanJiId());
                user.setBanjiName(banJi.getName());
            }
            if(user.getXueYuanId()!=null){
                //学院查库
                XueYuan xueYuan = xueYuanService.getById(user.getXueYuanId());
                user.setXueYuanName(xueYuan.getName());
            }
            if(user.getTeacherId()!=null){
                //学院查库
                User teacher = userService.getById(user.getTeacherId());
                user.setTeacherName(teacher.getUsername());
            }
        }
        if(userPage!=null){
            return new ResponseResult(ResponseCode.SUCCESS, Math.toIntExact(page.getTotal()),page.getRecords());
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 新增用户
    * */
    @PostMapping("/user/addUser")
    public ResponseResult addUser(User user){
        boolean b = userService.save(user);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 初始化化下拉列表 [班级]
    * */
    @GetMapping("/user/listAllBanJi")
    public ResponseResult listAllBanJi(){
        List<BanJi> list = banjiService.list();
        if(list!=null && list.size()>0){
            return new ResponseResult(ResponseCode.SUCCESS,list);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
     * 初始化化下拉列表 [学院]
     * */
    @GetMapping("/user/listAllXueYuan")
    public ResponseResult listAllXueYuan(){
        List<XueYuan> list = xueYuanService.list();
        if(list!=null && list.size()>0){
            return new ResponseResult(ResponseCode.SUCCESS,list);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    *修改用户
    * */
    @PostMapping("/user/updateUser")
    public ResponseResult updateUser(User user){
        boolean b = userService.updateById(user);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
     *删除用户
     * */
    @PostMapping("/user/deleteUser/{id}")
    public ResponseResult deleteUser(User user){
        boolean b = userService.removeById(user.getId());
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 重置密码
    * */
    @PostMapping("/user/resetPwd/{id}")
    public ResponseResult resetPwd(@PathVariable("id") Integer id){
        User user = new User();
        user.setId(id);
        user.setPassword("123456");
        boolean b = userService.updateById(user);
        if(b){
            return new ResponseResult(ResponseCode.SUCCESS);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 初始化用户角色列表
    * */
    @GetMapping("/user/initRoleByUserId")
    public ResponseResult initRoleByUserId(Integer id){
        //1.查询所有角色
        List<Map<String, Object>> listMaps = roleService.listMaps();
        //2.查询当前登录用户拥有的角色
        List<Integer> currentUserRoleIds = roleService.queryUserRoleById(id);
        //3.让前端 变为选中状态
        for (Map<String, Object> map : listMaps) {
            Boolean LAY_CHECKED = false;
            Integer roleId = (Integer) map.get("id");
            for (Integer rid : currentUserRoleIds) {
                if(rid.equals(roleId)){
                    LAY_CHECKED = true;
                    break;
                }
            }
            map.put("LAY_CHECKED",LAY_CHECKED);
        }
        if(listMaps!=null && listMaps.size()>0){
            return new ResponseResult(ResponseCode.SUCCESS,listMaps.size(),listMaps);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

    /*
    * 给用户分配角色
    * */
    @PostMapping("/user/saveUserRole")
    public ResponseResult saveUserRole(Integer uid,Integer[] ids){
        userService.saveUserRole(uid,ids);
        return new ResponseResult(ResponseCode.SUCCESS);
    }


    /*
    * 用户名失去焦点事件
    * */
    @GetMapping("/user/changeChineseToPinyin")
    public ResponseResult changeChineseToPinyin(String username){
       User user =  userService.selectUserByUsername(username);
        HashMap<String, String> map = new HashMap<>();
       if(user!=null){
           map.put("value",user.getUsername());
           return new ResponseResult(ResponseCode.SUCCESS,map);
       }else {
           return new ResponseResult(ResponseCode.FAILED);
       }
    }

    /*
    * 获取当前个人信息
    * */
    @GetMapping("/user/getNowUser")
    public ResponseResult getNowUser(String loginname){
        User user = userService.selectUserByUsername(loginname);
        if(user!=null){
            return new ResponseResult(ResponseCode.SUCCESS,user);
        }else {
            return new ResponseResult(ResponseCode.FAILED);
        }
    }

}
